/*
DigitalFlipbook Source Code License Agreement
Copyright(c) 2008 DigitalFlipbook. All rights reserved.
	
Please read this Source Code License Agreement carefully before using
the source code.
	
DigitalFlipbook grants to you a perpetual, worldwide, non-exclusive,
no-charge, royalty-free, irrevocable copyright license, to reproduce,
prepare derivative works of, publicly display, publicly perform, and
distribute this source code and such derivative works in source or
object code form without any attribution requirements.
	
The name "DigitalFlipbook" must not be used to endorse or promote products
derived from the source code without prior written permission.
	
You agree to indemnify, hold harmless and defend DigitalFlipbook from and
against any loss, damage, claims or lawsuits, including attorney's
fees that arise or result from your use or distribution of the source
code.
	
THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL DIGITALFLIPBOOK
OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.digitalflipbook.webapis.scribd.methodgroups
{
	import com.digitalflipbook.webapis.scribd.NameValuePair;
	import com.digitalflipbook.webapis.scribd.ScribdService;
	import com.digitalflipbook.webapis.scribd.events.ScribdResultEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	
	/**
	 * Broadcast as a result of the login method being called
	 *
	 * The event contains the following properties
	 *	success	- Boolean indicating if the call was successful or not
	 *	data - When success is true, contains a "user_login_response" string
	 *		   When success is false, contains an "error" ScribdError instance
	 *
	 * @see #login
	 * @see com.digitalflipbook.service.scribd.ScribdError
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 * @tiptext
	 */
	[Event( name="login", 
		 type="com.digitalflipbook.webapis.scribd.events.ScribdResultEvent.USER_LOGIN" )]
	
	/**
	 * Broadcast as a result of the signup method being called
	 *
	 * The event contains the following properties
	 *	success	- Boolean indicating if the call was successful or not
	 *	data - When success is true, contains a "user_signup_response" string
	 *		   When success is false, contains an "error" ScribdError instance
	 *
	 * @see #signup
	 * @see com.digitalflipbook.service.scribd.ScribdError
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 * @tiptext
	 */
	[Event( name="signup", 
		 type="com.digitalflipbook.webapis.scribd.events.ScribdResultEvent.USER_SIGNUP" )]
	
	public class User
	{
		/** 
		 * A reference to the ScribdService that contains the api key
		 * and logic for processing API calls/responses
		 */
		private var _service:ScribdService;
		
		public function User( service:ScribdService )
		{
			_service = service;
		}
		
		/**
		 * This method allows your API application to sign in as an existing Scribd user, executing methods as that user.
		 * 
		 * @param username The username of the user to login.
		 * 				The API actually handles this parameter flexibly, and will accept an email address instead of a username.
		 * @param password The password for the user account.
		 * @see http://www.scribd.com/platform/documentation/api?method_name=user.login
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 9.0
		 * @tiptext
		 */
		public function login( username:String, password:String ):void
		{
			// Let the Helper do the work to invoke the method			
			MethodGroupHelper.invokeMethod( _service, login_result,
									"user.login",
									true,
									new NameValuePair( "username", username ),
									new NameValuePair( "password", password ) );
		}
		
		/**
		 * Capture the result of the login call, and dispatch
		 * the event to anyone listening.
		 *
		 * @param event The complete event generated by the URLLoader
		 * 			that was used to communicate with the Scribd API
		 *			from the invokeMethod method in MethodGroupHelper
		 */
		private function login_result( event:flash.events.Event ):void
		{
			// Create a USER_LOGIN event
			var result:ScribdResultEvent = new ScribdResultEvent( ScribdResultEvent.USER_LOGIN );

			// Have the Helper handle parsing the result from the server - get the data
			// from the URLLoader which corresponds to the result from the API call
			MethodGroupHelper.processAndDispatch( _service, 
												  URLLoader( event.target ).data, 
												  result,
												  "user_login_response",
												  MethodGroupHelper.parseUserLogin );
		}
		
		/**
		 * This method allows your API application to signup a new Scribd user.
		 * If the signup is successful, your application will be passed back a session key which will allow you to execute methods on behalf of the new user.
		 * 
		 * @param username The username of the user to signup.
		 * @param password The password for the new user account.
		 * @param email The email address of the user. Please ensure that this address is valid to comply with US Copyright Law.
		 * @param name The real name of the user.
		 * @see http://www.scribd.com/platform/documentation/api?method_name=user.login
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 9.0
		 * @tiptext
		 */
		public function signup( username:String, password:String, email:String, name:String=null ):void
		{
			// Let the Helper do the work to invoke the method			
			MethodGroupHelper.invokeMethod( _service, signup_result,
									"user.signup",
									true,
									new NameValuePair( "username", username ),
									new NameValuePair( "password", password ),
									new NameValuePair( "email", email ),
									new NameValuePair( "name", name ) );
		}
		
		/**
		 * Capture the result of the login call, and dispatch
		 * the event to anyone listening.
		 *
		 * @param event The complete event generated by the URLLoader
		 * 			that was used to communicate with the Scribd API
		 *			from the invokeMethod method in MethodGroupHelper
		 */
		private function signup_result( event:flash.events.Event ):void
		{
			// Create a USER_SIGNUP event
			var result:ScribdResultEvent = new ScribdResultEvent( ScribdResultEvent.USER_SIGNUP );

			// Have the Helper handle parsing the result from the server - get the data
			// from the URLLoader which corresponds to the result from the API call
			MethodGroupHelper.processAndDispatch( _service, 
												  URLLoader( event.target ).data, 
												  result,
												  "user_signup_response",
												  MethodGroupHelper.parseUserSignup );
		}
		
	}
}
